def patrick(number):
    for i in range(2, int(number**0.5) + 1):
        if number % i == 0:
            return False
    return True
def max_patrick(number):
    if number > 9999 or number < 1000:
        return 0

    a4 = number // 1000
    a3 = (number % 1000) // 100
    a2 = (number % 100) // 10
    a1 = number % 10
    maximum = 0
    for cur in (a1, a2, a3, a4, a4*10 + a3, a3*10 + a2, a2*10 + a1, a4*100 + a3*10 + a2, a3*100 + a2*10 + a1, number):
        if patrick(cur) and cur > maximum:
            maximum = cur
    return maximum

number = int(input())
result = max_patrick(number)
print(result)
